package com.chixing.controller;

import com.chixing.pojo.Customer;
import com.chixing.pojo.Hotel;
import com.chixing.service.HotelService;
import com.chixing.service.HouseaddService;
import com.chixing.util.ServerResponse;
import com.mysql.cj.protocol.x.Notice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;


/**
 * @author dwe
 * @date 2021-10-21 20:23
 */
@Controller
public class HouseaddController {
    @Autowired
    private HouseaddService houseaddService;

    @PostMapping("houseadd")
    public ModelAndView save(Hotel hotel/*, HttpSession session*/){
       /* Customer customer = (Customer) session.getAttribute("loginCustomer");*/

        ModelAndView mav = new ModelAndView();
        //调用sevice保存 hotel对象
        ServerResponse serverResponse = houseaddService.saveHouse(hotel);

    /*    mav.addObject("serverResponse",serverResponse);
        mav.setViewName("house_owner/houseadd");*/

        if (serverResponse.getCode()==200){//添加成功
            //查询刚刚添加的房源详情
            ServerResponse serverResponse1 = houseaddService.getByHouseIdAndHousewonName(hotel.getHotelName());
            //绑定数据到页面中，页面跳转
            mav.addObject("serverResponse",serverResponse);
            mav.setViewName("house_owner/detail");
        }else {//添加失败,跳转 到添加页面
            mav.addObject("addMessage","添加失败");
            mav.setViewName("house_owner/houseadd");
        }
        return mav;
    }

    /*根据房东ID查询，查询所有房屋，分页查询*/
    @GetMapping(value = {"house_owner/{housewonId}/hotel","house_owner/{housewonId}/hotel/page/{pageNum}"})
    public ModelAndView getAll(@PathVariable("housewonId")Integer housewonId,
                               @PathVariable(value = "pageNum",required = false)Integer pageNum){
        if (pageNum == null)
            pageNum = 1;
        ModelAndView mav = new ModelAndView();
        ServerResponse serverResponse = houseaddService.getByHousewonNameAndPage(housewonId,pageNum);
        mav.addObject("serverResponse",serverResponse);
        mav.addObject("housewonId",housewonId);
        mav.setViewName("house_owner/all");
        return mav;
    }
}
